// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-6-24

#include "stdafx.h"
#include "wndcontainer.h"
#include ".\wndcontainer.h"

IMPLEMENT_DYNAMIC(CJoyWndContainer, CJoyWnd)

//##ModelId=44CF17B50257
CJoyWndContainer::CJoyWndContainer(void)
    : m_margin(0, 0, 0, 0)
{
    m_pWnd = NULL;
}

//##ModelId=44CF17B50259
CJoyWndContainer::~CJoyWndContainer(void)
{
}

//##ModelId=44CF17B50263
void CJoyWndContainer::SetWnd(CWnd* pWnd)
{
    m_pWnd = pWnd;
    CRect cr;
    GetClientRect(&cr);
    ValidateArea(cr.Width(), cr.Height());
}

//##ModelId=44CF17B50261
void CJoyWndContainer::SetMargin(const CRect& margin)
{
    if(m_margin != margin)
    {
        m_margin = margin;
        CJoyCtrl::InvalidateCtrl();
    }
}

//##ModelId=44CF17B5024A
void CJoyWndContainer::DoAlignChilds(CRect& remain)
{
    CRect newRemain(
        remain.left+m_margin.left,
        remain.top+m_margin.top,
        remain.right-m_margin.right,
        remain.bottom-m_margin.bottom);
    __super::DoAlignChilds(newRemain);
}

//##ModelId=44CF17B50247
void CJoyWndContainer::ValidateArea(int cx, int cy)
{
    if(m_pWnd == NULL)
    {
        return;
    }
    CRect area;
    area.left   = m_margin.left;
    area.top    = m_margin.top;
    area.right  = cx - m_margin.right;
    area.bottom = cy - m_margin.bottom;

    m_pWnd->MoveWindow(&area);
}

BEGIN_MESSAGE_MAP(CJoyWndContainer, CJoyWnd)
    ON_WM_SIZE()
    ON_WM_DESTROY()
END_MESSAGE_MAP()

//##ModelId=44CF17B50251
void CJoyWndContainer::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
    __super::CM_LoadSkin(pSkinElem);

    CString strMargin = pSkinElem->ReadString("margin");
    if(!strMargin.IsEmpty())
    {
        sscanf(strMargin, "%d,%d,%d,%d", 
            &m_margin.left, &m_margin.top, &m_margin.right, &m_margin.bottom);
    }
}

//##ModelId=44CF17B50254
void CJoyWndContainer::CM_Draw(Graphics& graph)
{
    if(m_backColor.GetValue() == 0)
    {
        return;
    }

    Rect r;
    SolidBrush brush(m_backColor);

    r.Width  = m_area.Width();
    r.X = 0;
    if(m_margin.top > 0)
    {
        r.Height = m_margin.top;
        r.Y = 0;
        graph.FillRectangle(&brush, r);
    }
    if(m_margin.bottom > 0)
    {
        r.Height = m_margin.bottom;
        r.Y = m_area.Height()-m_margin.bottom;
        graph.FillRectangle(&brush, r);
    }

    r.Height = m_area.Height()-m_margin.top-m_margin.bottom;
    r.Y = m_margin.top;
    if(m_margin.left > 0)
    {
        r.Width = m_margin.left;
        r.X = 0;
        graph.FillRectangle(&brush, r);
    }
    if(m_margin.right > 0)
    {
        r.Width = m_margin.right;
        r.X = m_area.Width()-m_margin.right;
        graph.FillRectangle(&brush, r);
    }
    //graph.DrawRectangle(
}

//##ModelId=44CF17B50267
void CJoyWndContainer::OnSize(UINT nType, int cx, int cy)
{
    CJoyWnd::OnSize(nType, cx, cy);

    ValidateArea(cx, cy);
}

//##ModelId=44CF17B50272
void CJoyWndContainer::OnDestroy()
{
    CJoyWnd::OnDestroy();

    // TODO: Add your message handler code here
}
